package com.report.config;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.report.constants.TagConstants;
import com.report.service.IMemberService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

/**
 * @author summer
 * @version 1.0
 * @date 2024/8/14 14:29
 */
@Configuration
@Slf4j
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    public RedisKeyExpirationListener(RedisMessageListenerContainer redisMessageListenerContainer) {
        super(redisMessageListenerContainer);
    }



    @Override
    public void onMessage(Message message, byte[] pattern) {
        String string = message.toString();
        String memberId = StrUtil.subAfter(string,TagConstants.KEY_EXPIRATION, true);
        log.info("过期redis----->key:{}",memberId);
        if(StrUtil.isNotBlank(memberId)){
            IMemberService memberService = SpringUtil.getBean(IMemberService.class);
            memberService.updateMemberState(Convert.convert(Integer.class,memberId));
        }
        super.onMessage(message, pattern);
    }


}
